import * as types from '../mutation-types.js'
import apiurl from '@/api/apiurl'
// import Api from '@/api/index'
import { AuthenticationUserInfo } from '@/const'

//全局状态
const state = {
  username: '',
  password: '',
  tabData: [],
  treeData: [],
  token: '',
  isModule: 'prove',
  type: 3, //用户类别：1.admin 2.应用员 3.检视员
  userType: '檢視員',
  groupId: '', //群组id
  groupName: '', //群组名
  times: 5 //倒计时获取报警列表
}

//getters
const getters = {
  username: state => {
    return state.username
  },
  password: state => {
    return state.password
  },
  tabData: state => {
    return state.tabData
  },
  treeData: state => {
    return state.treeData
  },
  token: state => {
    return state.token
  },
  isModule: state => {
    return state.isModule
  },
  type: state => {
    return state.type
  },
  userType: state => {
    return state.userType
  },
  groupId: state => {
    return state.groupId
  },
  groupName: state => {
    return state.groupName
  },
}

const mutations = {
  [types.SET_USERNAME] (state, username) {
    state.username = username
  },
  [types.SET_PASSWORD] (state, password) {
  	state.password = password
  },
  [types.SET_TABDATA](state, tabData) {
    state.tabData = tabData
  },
  [types.SET_TREEDTA](state, treeData) {
    state.treeData = treeData
  },
  [types.SET_TOKEN](state, token) {
    state.token = token
  },
  [types.SET_MODULE](state, isModule) {
    state.isModule = isModule
  },
  [types.SET_TYPE](state, type) {
    state.type = type
    state.userType = type == 1 ? 'admin' : type == 2 ? '應用員' : '檢視員'
  },
  [types.SET_GROUPID](state, groupId) {
    state.groupId = groupId
  },
  [types.SET_GROUPNAME](state, groupName) {
    state.groupName = groupName
  },
  [types.SET_TIMES](state, times) {
    state.times = times
  }
}

const actions = {
  /**
   * 注册
   * @param params{ account: 账号, password: 密码, name: 名称 }
   * @returns { status: 1, result: {}, message: '注册成功' }
   */
  async fetchRegister(_, params = {}) {
    // console.log(params,'注册参数')
    // console.log(apiurl.register)
    let res = await Api.request(apiurl.register, {
      ...params
    })
    return res
  },

  /**
   * 登录
   * @param params{ account: 账号, password: 密码 }
   * @returns { status: 1, result: { name: 名称, id: 唯一标识, account: 账号, type: 类型(1.管理员 2.应用员 3.檢視員), groupId: 群组id, groupName: 群组名称  }, message: '登录成功' }
   */
  async fetchLogin({ commit }, params = {}) {
    // console.log(params,'登录参数')
    // console.log(apiurl.adminLogin)
    let res = await Api.request(apiurl.adminLogin, {
      ...params
    })
    // console.log(res)
    //默认给最低的权限
    commit(types.SET_TYPE, res.result.type || 3)
    // commit(types.SET_MODULE, (res.result.account === 'admin' || res.result.account === 'admin2') ? 'admin' : process.env.VUE_APP_MODULE)
    commit(types.SET_GROUPID, res.result.groupId)
    commit(types.SET_GROUPNAME, res.result.groupName)
    return res
  },

  /**
   * 修改密码？理论上要校验旧密码
   * @param params{ account: 账号, password: 密码, type: 类型 1.管理员 2.应用员 3.检视员 }
   * @returns { status: 1, result: {}, message: '添加子账号成功' }
   */
  async fetchUpdatePwd({ state }, params = {}) {
    // console.log(params,'修改密码参数')
    // console.log(apiurl.adminChangePassword)
    let res = await Api.request(apiurl.adminChangePassword, {
      ...params,
      type: params.type || state.type
    })
    return res
  },

  /**
   * 鉴证员登录
   * @param params{ account: 账号, password: 密码 }
   * @returns { status: 1, result: { name: 名称, id: 唯一标识, account: 账号 }, message: '登录成功' }
   */
  async fetchWitnessLogin({ commit }, params = {}) {
    // console.log(params,'鉴证员登录参数')
    // console.log(apiurl.witnessLanding)
    let isProve = AuthenticationUserInfo.find(item => {
      return (item.name == params.account) && (item.value == params.password)
    })
    if(!isProve) {
      return { status: 0, result: { }, message: '非鑒證員帳號或密碼錯誤！' }
    }
    let res = await Api.request(apiurl.witnessLanding, {
      ...params
    })
    commit(types.SET_TYPE, 4)
    return res
  },
  
  fetchTimes({ state, commit }, params = '') {
    commit(types.SET_TIMES, params)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  getters,
  actions
}
